import 'package:flutter/material.dart';
import 'package:flutter_hsh/brick/brick.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../style/style.dart';

class BottomBar extends StatelessWidget {
  const BottomBar({Key? key, this.index = 0, required this.items, required this.unReadCount}) : super(key: key);
  final int index;
  final int unReadCount;
  final List<BottomBarItem> items;

  @override
  Widget build(BuildContext context) {
    return UIContainer(
      padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
      height: 58.h + ScreenUtil().bottomBarHeight,
      decoration: BoxDecoration(
        border: const Border(top: BorderSide(color: Colors.white, width: 0.5)),
        color: kAppColor('#22223A'),
        boxShadow: [
          BoxShadow(color: kAppColor('#000000').withOpacity(0.07), offset: const Offset(0, -2), blurRadius: 14, spreadRadius: 0),
        ],
      ),
      child: Row(
        children: List.generate(
            items.length,
            (index) => _buildItemView(
                  i: index,
                  item: items.elementAt(index),
                )).toList(),
      ),
    );
  }

  Widget _buildItemView({required int i, required BottomBarItem item}) => Expanded(
        child: GestureDetector(
          onTap: () {
            if (item.onClick != null) item.onClick!(i);
          },
          behavior: HitTestBehavior.translucent,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Positioned(
                top: 39.h,
                child: Text(
                  item.label,
                  style: i == index
                      ? (item.selectedStyle ??
                          TextStyle(
                            fontSize: 12.sp,
                            color: const Color(0xFF1B72EC),
                          ))
                      : (item.unselectedStyle ??
                          TextStyle(
                            fontSize: 12.sp,
                            color: const Color(0xFF1B72EC),
                          )),
                ),
              ),
              Positioned(
                top: 8.h,
                child: UIImage(
                  assetImage: i == index ? item.selectedImgRes : item.unselectedImgRes,
                  fit: BoxFit.contain,
                  width: item.imgWidth,
                  height: item.imgHeight,
                ),
              ),
              // if (i == 1)
              if (item.count != null && item.count! > 0)
                Positioned(
                  top: 3.h,
                  child: UIText(
                    text: '${item.count! > 99 ? '99+' : item.count}',
                    fontSize: 10.sp,
                    margin: EdgeInsets.only(left: 12.w),
                    alignment: Alignment.center,
                    constraints: BoxConstraints(minHeight: 16.w, minWidth: 16.w),
                    textColor: Colors.white,
                    color: kAppColor('#F74C31'),
                    radius: 100.w,
                  ),
                ),
            ],
          ),
        ),
      );
}

class BottomBarItem {
  final String selectedImgRes;
  final String unselectedImgRes;
  final String label;
  final TextStyle? selectedStyle;
  final TextStyle? unselectedStyle;
  final double imgWidth;
  final double imgHeight;
  final Function(int index)? onClick;
  final Stream<int>? stream;
  final int? count;

  BottomBarItem(
      {required this.selectedImgRes,
      required this.unselectedImgRes,
      required this.label,
      this.selectedStyle,
      this.unselectedStyle,
      required this.imgWidth,
      required this.imgHeight,
      this.onClick,
      this.stream,
      this.count});
}
